package com.boot.security.server.service.impl.mffl.Mfflpublic;

import com.boot.security.server.dao.mffl.MfflConfigDao;
import com.boot.security.server.model.mffl.MfflConfig;
import com.boot.security.server.utils.mffl.Mfflconfig;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 公共功能业务
 */
@Service
public class publicService {

    //系统配置表
    @Autowired
    private MfflConfigDao mfflConfigDao;

    //获取移动端版本号，下载地址
    public ReturnBean getApplicationInfo(String applicationname,String code){
        ReturnBean returnBean = new ReturnBean();
        List<MfflConfig> byName = mfflConfigDao.getLikeName(applicationname);
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        for(MfflConfig item :byName ){
            stringStringHashMap.put(item.getName(),item.getValue());
        }
        String string = Mfflconfig.Android_code.name().contains(applicationname) ? Mfflconfig.Android_code.name() : Mfflconfig.IOS_code.name();
        Integer versioncode = Integer.parseInt(stringStringHashMap.get(string));
        if(versioncode == Integer.parseInt(code)){
             returnBean.setMessage("当前版本为最新版本，无需更新");
            return returnBean;
        }

        returnBean.setDatas(stringStringHashMap);
        return returnBean;
    }

    //获取联系我们信息
    public  ReturnBean  getContactusInfo(){
        ReturnBean returnBean = new ReturnBean();
        List<MfflConfig> byName = mfflConfigDao.getLikeName("Contactus");
        HashMap<String, String> map = new HashMap<>();

        for(MfflConfig item:byName){
            map.put(item.getName(),item.getValue());
        }
        returnBean.setDatas(map);
        return returnBean;
    }


    /**
     *
     * @param uploadsize 上传图片大小
     * @return 规定尺寸返回true,不符合规定返回false
     */
     public boolean uploadImageSize(Long uploadsize){
         List<MfflConfig> likeName = mfflConfigDao.getLikeName(Mfflconfig.Upload_image.name());
         Long configImageSize = 5120L;
         if(likeName.size() == 0 || likeName == null){
             if(uploadsize >= configImageSize){
                 return false;
             }
         }
         if(uploadsize >= Long.parseLong(likeName.get(0).getValue())){
             return false;
         }
         return true;
     }

    /**
     * 拼接推送消息
     * @message 消息
     * @paymerchantnumber 商户订单号
     * @return
     */
     public String MessageModel(String message,String paymerchantnumber){
         List<MfflConfig> likeName = mfflConfigDao.getLikeName(Mfflconfig.Message_model.name());
         String format = MessageFormat.format(likeName.get(0).getValue(), message, paymerchantnumber);
         return format;
     }
}
